// packageTab/pages/securities/securities.js
import {
	gift_query,
	gift_cancel,
} from '../../../utils/imapi.js';
var app = getApp()
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		tabIndex1: 0,
		text_value: "",
		tabs: [{
				"id": 1,
				"name": "待使用",
				"p": "",
				"is_selected": 1
			},
			{
				"id": 2,
				"name": "已使用",
				"p": "JD",
				"is_selected": 0
			},
			{
				"id": 3,
				"name": "过期劵",
				"p": "JD",
				"is_selected": 2
			},
		],
		sliderLeft1: 170,
		sliderOffset1: 375,
		id: 0, //tab选中的
		list: [], //数据的列表
		page_index: 2, //页数
		loading: false, //是否是加载中
		userFeedbackHidden: false, // 默认隐藏
		feedbackContent: '', // 用户反馈内容
		item: {},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function(options) {
		let data = await gift_query({
			status: 1,
			page_index: 1,
			page_size: 10
		});
		this.setData({
			list: data.data
		})
	},
	bindName(e) {
		//方法一
		var value = e.detail.value;
		this.setData({
			text_value: value
		})
	},
	async gift(status) {
		let data = await gift_query({
			status: status,
			page_index: 1,
			page_size: 10
		});
		this.setData({
			list: data.data
		})
	},
	onTabClick1(e) {
		if (this.data.id == e.detail.index) {
			return;
		} else if (e.detail.index == 0 || e.detail.index == 1) {
			this.gift(e.detail.index + 1);
			this.setData({
				page_index: 2
			});
		} else {
			this.gift(e.detail.index + 2);
			this.setData({
				page_index: 2
			});
		}
		this.setData({
			id: e.detail.index
		})
	},
	async live_code(status, page_index) {
		let data = await gift_query({
			status: status,
			page_index: page_index,
			page_size: 10
		});
		this.setData({
			list: this.data.list.concat(data.data),
		})
	},
	exchange(e) {
		console.log(e);
		var item = e.currentTarget.dataset.item;
		console.log(item);
		wx.navigateTo({
			url: '/packageTab/pages/exchange/exchange?item=' + JSON.stringify(item)
		})
	},
	async refund(e) {
		var item = e.currentTarget.dataset.item;
		wx.showModal({
			title: '提示',
			content: '您确定要退卷?',
			success: (res) => {
				if (res.confirm) {
					this.cancel(item);
				} else {
					console.log('用户点击取消')
				}

			}
		})
	},
	Sendsecurities(e) {
		var item = e.currentTarget.dataset.item;
		// wx.navigateTo({
		//   url: '/packageTab/pages/Sendsecurities/Sendsecurities?item='+JSON.stringify(item)
		// })
		//console.log(app.globalData.userInfo.name);
		this.setData({
			userFeedbackHidden: true,
			item: item
		})
	},
	async cancel(item) {
		let data = await gift_cancel({
			gift_no: item.gift_no
		});
		console.log(data);
		if (data.code === 0) {
			wx.showToast({
				title: '退劵成功',
				icon: 'success',
				duration: 2000 //持续的时间
			})
		}
	},
	hideDiv() { // 隐藏输入弹出框
		this.setData({
			userFeedbackHidden: false
		})
	},
	submitFeedback() { // 提交反馈
		// 提交反馈内容
		console.log(this.data.feedbackContent);
	},
	setClip(e) {
		var item = e.currentTarget.dataset.id;
		wx.setClipboardData({
			data: item,
			success: function(res) {
				wx.showToast({
					title: '复制成功'
				})
			}
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		this.setData({
			loading: true
		});
		if (!this.data.loading) {
			return;
		}
		if (this.data.id == 0 || this.data.id == 1) {
			this.live_code(this.data.id + 1, this.data.page_index);
			this.setData({
				page_index: this.data.page_index + 1
			})
		} else {
			this.live_code(this.data.id + 2, this.data.page_index);
			this.setData({
				page_index: this.data.page_index + 1
			})
		}
		setTimeout(() => {
			this.setData({
				loading: false
			});
		}, 500);

	},

	/**
	 * 用户点击右上角分享
	 */
	// onShareAppMessage: function() {
	// 	console.log('是这个');
	// 	if (!app.is_auth()){
	// 	  wx.showToast({
	// 	    title: '请先登陆后，才能分享',
	// 	  })
	// 	  // 请先登陆
	// 	  wx.navigateTo({
	// 	    url: '/pages/login/login?' + jsonParamToQueryStr(this.data.options),
	// 	  })
	// 	  return 
	// 	}
	// 	if (options.from=='button'){
	// 	  console.log('按钮分享:',options.target)
	// 	}else {

	// 	}
	//          var path='/pages/tabbar/index/index'
	//        return {
	// 		imageUrl: '/images/tabbar/ties.png',
	// 		path:path,
	// 		title: '111星品劵111'
	//        }
	// },
	onShareAppMessage(options) {
		console.log(app.globalData.userInfo);
		this.setData({
			userFeedbackHidden: false
		})
		if (!app.is_auth()) {
			wx.showToast({
				title: '请先登陆后，才能分享',
			})

			wx.navigateTo({
				url: '/pages/login/login'
			})
			return
		}
		var path = '/packageTab/pages/Sendsecurities/Sendsecurities?item=' + JSON.stringify(this.data.item) +
			'&text_value=' + JSON.stringify(this.data.text_value) + '&name=' + app.globalData.userInfo.name

		return {
			imageUrl: this.data.item.pict_main_url,
			path: path,
			title: '星品劵'
		}
	}
})
